package com.lyfei.ydc.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyfei.ydc.core.LocalUser;
import com.lyfei.ydc.core.interceptors.ScopeLevel;
import com.lyfei.ydc.domain.dto.OrderDto;
import com.lyfei.ydc.domain.entity.Order;
import com.lyfei.ydc.domain.vo.OrderIdVo;
import com.lyfei.ydc.domain.vo.OrderSimplifyVo;
import com.lyfei.ydc.domain.vo.OrderVo;
import com.lyfei.ydc.exception.http.NotFoundException;
import com.lyfei.ydc.logic.OrderChecker;
import com.lyfei.ydc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/placeOrder")
    @ScopeLevel()
    public OrderIdVo placeOrder(@RequestBody OrderDto orderDto) {
        Long uid = LocalUser.getUser().getId();
        OrderChecker orderChecker = this.orderService.isOk(uid, orderDto);

        Long oid = this.orderService.placeOrder(uid, orderDto, orderChecker);
        return new OrderIdVo(oid);

    }

    @ScopeLevel
    @GetMapping("/by/status/{status}")
    public IPage<OrderSimplifyVo> getByStatus(@PathVariable Integer status,
                                              @RequestParam("start") Integer start,
                                              @RequestParam("count") Integer count) {
        int current = start / count + 1;
        return orderService.getByStatus(current, count, status);
    }

    @ScopeLevel
    @GetMapping("/status/unpaid")
    public IPage<OrderSimplifyVo> getUnpaid(@RequestParam("start") Integer start,
                                            @RequestParam("count") Integer count) {
        int current = start / count + 1;
        return orderService.getUnpaid(current, count);
    }

    @ScopeLevel
    @GetMapping("/detail/{id}")
    public OrderVo getOrderDetail(@PathVariable(name = "id") Long oid) {
        return orderService.getOrderDetail(oid);
    }

}
